package com.af.monitorModule.controller.user;

import com.af.monitorModule.common.ResultEnum;
import com.af.monitorModule.generator.service.UserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录请求
     * @param data
     * @return
     */
    @PostMapping("/getLogin")
    public JSONObject getLogin(@RequestBody JSONObject data){
        return userService.Login(data);
    }


    /**
     * 获取对应用户的菜单树
     * @param data
     * @return
     */
    @PostMapping("/getTree")
    public JSONObject getTree(@RequestBody JSONObject data){
        return userService.getTree(data);
    }

    /**
     * 退出登录
     * @param data
     * @return
     */
    @PostMapping("/checkOut")
    public JSONObject checkOut(@RequestBody JSONObject data){
        return userService.checkOut(data);
    }

    /**
     * 获取所有的用户列表
     * @param page
     * @param limit
     * @param search
     * @return
     */
    @GetMapping("/getAllAccount")
    public JSONObject getAllAccount(String page,String limit,String search){
        JSONObject searchData = JSONObject.parseObject(search);
        return userService.getAllAccount(page,limit,searchData);
    }

    @PostMapping("/changeState/{id}")
    public JSONObject changeState(@PathVariable("id") String id){
        return userService.changeState(id);
    }


    @GetMapping("/checkToken")
    public JSONObject checkToken(){
        return ResultEnum.OK.getResult();
    }

    @PostMapping("/creat")
    @Transactional
    public JSONObject creat(@RequestBody JSONObject data){
        String[] users = data.getString("name").split(",");
        String bumen = data.getString("bumen");
        for (String user : users) {
            userService.creatUser(user,bumen);
        }
        return ResultEnum.OK.getResult();
    }
}
